package service

import (
	"context"
	__ "user-server/basic/proto"
	"user-server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {

	user := model.User{
		Username: in.Username,
		Password: in.Password,
		Mobile:   in.Mobile,
	}

	if err := user.CreateUser(); err != nil {
		return &__.RegisterResp{
			Code: 400,
			Msg:  "用户创建失败",
		}, nil
	}

	return &__.RegisterResp{
		Code: 200,
		Msg:  "注册成功",
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {

	user := model.User{
		Password: in.Password,
		Mobile:   in.Mobile,
	}

	if user.ID == 0 {
		return &__.LoginResp{
			Code: 400,
			Msg:  "用户不存在",
		}, nil
	}

	return &__.LoginResp{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}
